package com.xinke.module.system.controller.admin.mail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.mail.*;
import com.xinke.module.system.model.entity.mail.MailTemplateDO;
import com.xinke.module.system.service.mail.MailTemplateService;

@Tag(name = "管理后台 - 邮件模版")
@RestController
@RequestMapping("/system/mail-template")
@Validated
public class MailTemplateController {

    @Resource
    private MailTemplateService mailTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建邮件模版")
    @PreAuthorize("@ss.hasPermission('system:mail-template:create')")
    public Result<Long> createMailTemplate(@Valid @RequestBody MailTemplateSaveReqVO createReqVO) {
        return success(mailTemplateService.createMailTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新邮件模版")
    @PreAuthorize("@ss.hasPermission('system:mail-template:update')")
    public Result<Boolean> updateMailTemplate(@Valid @RequestBody MailTemplateSaveReqVO updateReqVO) {
        mailTemplateService.updateMailTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邮件模版")
    @Parameter(name = "templateId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:mail-template:delete')")
    public Result<Boolean> deleteMailTemplate(@RequestParam("templateId") Long templateId) {
        mailTemplateService.deleteMailTemplate(templateId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邮件模版")
    @Parameter(name = "templateId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:mail-template:query')")
    public Result<MailTemplateRespVO> getMailTemplate(@RequestParam("templateId") Long templateId) {
        MailTemplateDO mailTemplate = mailTemplateService.getMailTemplate(templateId);
        return success(BeanUtils.toBean(mailTemplate, MailTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邮件模版分页")
    @PreAuthorize("@ss.hasPermission('system:mail-template:query')")
    public Result<PageResult<MailTemplateRespVO>> getMailTemplatePage(@Valid MailTemplatePageReqVO pageReqVO) {
        PageResult<MailTemplateDO> pageResult = mailTemplateService.getMailTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MailTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出邮件模版 Excel")
    @PreAuthorize("@ss.hasPermission('system:mail-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMailTemplateExcel(@Valid MailTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MailTemplateDO> list = mailTemplateService.getMailTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "邮件模版.xls", "数据", MailTemplateRespVO.class,
                        BeanUtils.toBean(list, MailTemplateRespVO.class));
    }

}